﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Globalization;

namespace Atomic
{
    internal class ConnectionLogScope : IReadOnlyList<KeyValuePair<string, object>>
    {
        private readonly string _connectionId;

        private string _cachedToString;

        public ConnectionLogScope(string connectionId)
        {
            _connectionId = connectionId;
        }

        public KeyValuePair<string, object> this[int index]
        {
            get
            {
                if (index == 0)
                {
                    return new KeyValuePair<string, object>("ConnectionId", _connectionId);
                }

                throw new ArgumentOutOfRangeException(nameof(index));
            }
        }

        public int Count => 1;

        public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
        {
            for (int i = 0; i < Count; ++i)
            {
                yield return this[i];
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public override string ToString()
        {
            if (_cachedToString == null)
            {
                _cachedToString = string.Format(
                    CultureInfo.InvariantCulture,
                    "ConnectionId:{0}",
                    _connectionId);
            }

            return _cachedToString;
        }
    }
    ///// <summary>
    ///// Manages the generation of the date header value.
    ///// </summary>
    //internal class DateHeaderValueManager : IHeartbeatHandler
    //{
    //    // This uses C# compiler's ability to refer to static data directly. For more information see https://vcsjones.dev/2019/02/01/csharp-readonly-span-bytes-static
    //    private static ReadOnlySpan<byte> DatePreambleBytes => new byte[8] { (byte)'\r', (byte)'\n', (byte)'D', (byte)'a', (byte)'t', (byte)'e', (byte)':', (byte)' ' };

    //    private DateHeaderValues _dateValues;

    //    /// <summary>
    //    /// Returns a value representing the current server date/time for use in the HTTP "Date" response header
    //    /// in accordance with http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.18
    //    /// </summary>
    //    /// <returns>The value in string and byte[] format.</returns>
    //    public DateHeaderValues GetDateHeaderValues() => _dateValues;

    //    // Called by the Timer (background) thread
    //    public void OnHeartbeat(DateTimeOffset now)
    //    {
    //        SetDateValues(now);
    //    }

    //    /// <summary>
    //    /// Sets date values from a provided ticks value
    //    /// </summary>
    //    /// <param name="value">A DateTimeOffset value</param>
    //    private void SetDateValues(DateTimeOffset value)
    //    {
    //        var dateValue = HeaderUtilities.FormatDate(value);
    //        var dateBytes = new byte[DatePreambleBytes.Length + dateValue.Length];
    //        DatePreambleBytes.CopyTo(dateBytes);
    //        Encoding.ASCII.GetBytes(dateValue, dateBytes.AsSpan(DatePreambleBytes.Length));

    //        var dateValues = new DateHeaderValues
    //        {
    //            Bytes = dateBytes,
    //            String = dateValue
    //        };
    //        Volatile.Write(ref _dateValues, dateValues);
    //    }

    //    public class DateHeaderValues
    //    {
    //        public byte[] Bytes;
    //        public string String;
    //    }
    //}
}
